//
//  NuclideTable.m
//  MacHeliosSim
//
//  Created by Paul Thompson on 8/11/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "NuclideTable.h"


@implementation NuclideTable


- (id)init
{
	//[super init];
	//nuclearDataArray = [[NSMutableArray alloc]init];
	
	[self initWithDataString:nil];
	
	return self;
}



- (id)initWithDataString:(NSString *)theData
{
	[super init];
	nuclearDataArray = [[NSMutableArray alloc]init];
	if ( theData == nil ) return self;
    scanner = [NSScanner scannerWithString:theData];
    NSCharacterSet *skipMe = [NSCharacterSet characterSetWithCharactersInString:@"\n\r, "];
	[scanner setCharactersToBeSkipped:skipMe];
	
    int neutronCount,protonCount,nucleonCount;
	float deltaM, deltaDeltaM;
	NSString *symbol;
	Nuclide *temp;
	
	[scanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@"\n"] intoString:&symbol];

#ifdef DEBUG	
	NSLog(@"scooped stuff: %@",symbol);
#endif
	
	while ( ![scanner isAtEnd] )
	{
		[scanner scanInt:&neutronCount];
			[scanner scanInt:&protonCount];
		[scanner scanInt:&nucleonCount];
#ifdef DEBUG
		NSLog(@"neutronCount = \"%i\"",neutronCount);
		NSLog(@"protonCount = \"%i\"",protonCount);
		NSLog(@"nucleonCount = \"%i\"",nucleonCount);
#endif
		[scanner scanUpToCharactersFromSet:skipMe intoString:&symbol];
		[scanner scanFloat:&deltaM];
		[scanner scanFloat:&deltaDeltaM];
		
		temp = [[Nuclide alloc] initWithNeutronCount:neutronCount
										 ProtonCount:protonCount
										NucleonCount:nucleonCount
									   ElementSymbol:symbol
											  DeltaM:deltaM
										 DeltaDeltaM:deltaDeltaM];
		[nuclearDataArray addObject:temp];
    }
	
	return self;
	
}



- (id)fillWithData:(NSString *)theData
{
	if ( theData == nil ) return self;
    scanner = [NSScanner scannerWithString:theData];
    NSCharacterSet *skipMe = [NSCharacterSet characterSetWithCharactersInString:@"\n\r, "];
	[scanner setCharactersToBeSkipped:skipMe];
	
    int neutronCount,protonCount,nucleonCount;
	float deltaM, deltaDeltaM;
	NSString *symbol;
	Nuclide *temp;
	[scanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@"\n"] intoString:&symbol];
#ifdef DEBUG
	NSLog(@"scooped stuff: %@",symbol);
#endif
	
	while ( ![scanner isAtEnd] )
	{
		[scanner scanInt:&neutronCount];
		[scanner scanInt:&protonCount];
		[scanner scanInt:&nucleonCount];
#ifdef DEBUG
		NSLog(@"neutronCount = \"%i\"",neutronCount);
		NSLog(@"protonCount = \"%i\"",protonCount);
		NSLog(@"nucleonCount = \"%i\"",nucleonCount);
#endif
		[scanner scanUpToCharactersFromSet:skipMe intoString:&symbol];
		[scanner scanFloat:&deltaM];
		[scanner scanFloat:&deltaDeltaM];
		
		temp = [[Nuclide alloc] initWithNeutronCount:neutronCount
										 ProtonCount:protonCount
										NucleonCount:nucleonCount
									   ElementSymbol:symbol
											  DeltaM:deltaM
										 DeltaDeltaM:deltaDeltaM];
		[nuclearDataArray addObject:temp];
    }
	
	return self;
}



- (BOOL)clearArray
{
	[nuclearDataArray removeAllObjects];
	return YES;
}



- (NSString *) elementSymbolForProtonCount:(int) theProtonCount
{
#ifdef DEBUG
	NSLog(@"Entering method elementSymbolForProtonCount:");
#endif
	if (theProtonCount == nil) {
#ifdef DEBUG
		NSLog(@"EPIC FAIL! PROTON COUNT IS NIL!");
#endif
		return nil;
	}
	int theCount = [nuclearDataArray count];
#ifdef DEBUG
	NSLog(@"Number of items in array: %i",theCount);
#endif
	
	for (int x =0;x<theCount;x++)
	{
		if ([[nuclearDataArray objectAtIndex:x] protonCount] == theProtonCount) {
#ifdef DEBUG
			NSLog(@"\t\t\tFOUND IT!!!!");
			NSLog(@"%i",x);
#endif
			return [[nuclearDataArray objectAtIndex:x] elementSymbol]; 
		}
		
	}
	return nil;
}



- (int) protonCountForElementSymbol:(NSString *)theElementSymbol
{
#ifdef DEBUG
	NSLog(@"entering method protonCountForElementSymbol:");
#endif
	if (theElementSymbol == nil) {
#ifdef DEBUG
		NSLog(@"EPIC FAIL! theElementSymbol IS NIL!");
#endif
		return -256;
	}
	int theCount = [nuclearDataArray count];
#ifdef DEBUG
	NSLog(@"Number of items in array: %i",theCount);
#endif	
	for (int x =0;x<theCount;x++)
	{
		if ([theElementSymbol caseInsensitiveCompare:[[nuclearDataArray objectAtIndex:x] elementSymbol]] == 0) {
#ifdef DEBUG
			NSLog(@"\t\t\tFOUND IT!!!!");
			NSLog(@"%i",x);
#endif
			return [[nuclearDataArray objectAtIndex:x] protonCount]; 
		}
		
	}
	return -256;
}



- (Nuclide *) entryForNucleonCount:(int) theNucleonCount
							ProtonCount:(int) theProtonCount
{
#ifdef DEBUG
	NSLog(@"entering method entryForNucleonCount:ProtonCount:");
#endif
	if (theNucleonCount == nil || theProtonCount == nil) {
#ifdef DEBUG 
		NSLog(@"EPIC FAIL! theNucleonCount or theProtonCount IS NIL!");
#endif
return nil;
	}
	int theCount = [nuclearDataArray count];
#ifdef DEBUG
	NSLog(@"Number of items in array: %i",theCount);
#endif

	for (int x =0;x<theCount;x++)
	{
		if ([[nuclearDataArray objectAtIndex:x] nucleonCount] == theNucleonCount &&
			[[nuclearDataArray objectAtIndex:x] protonCount] == theProtonCount) {
#ifdef DEBUG
			NSLog(@"\t\t\tFOUND IT!!!!");
			NSLog(@"%i",x);
#endif
			return [nuclearDataArray objectAtIndex:x]; 
		}
		
	}
#ifdef DEBUG
	NSLog(@"Entry not found in nuclearDataArray for nucleon count %i and proton count %i",theNucleonCount,theProtonCount);
#endif
	return nil;
}



- (Nuclide *) entryForNeutronCount:(int) theNeutronCount
							ProtonCount:(int) theProtonCount
{
#ifdef DEBUG
	NSLog(@"entering method entryForNeutronCount:ProtonCount:");
#endif

	if (theNeutronCount == nil || theProtonCount == nil) {
#ifdef	DEBUG
		NSLog(@"EPIC FAIL! theNucleonCount or theProtonCount IS NIL!");
#endif

		return nil;
	}
	int theCount = [nuclearDataArray count];
#ifdef DEBUG
	NSLog(@"Number of items in array: %i",theCount);
#endif

	for (int x =0;x<theCount;x++)
	{
		if ([[nuclearDataArray objectAtIndex:x] neutronCount] == theNeutronCount &&
			[[nuclearDataArray objectAtIndex:x] protonCount] == theProtonCount) {
#ifdef DEBUG
			NSLog(@"\t\t\tFOUND IT!!!!");
			NSLog(@"%i",x);
#endif

			return [nuclearDataArray objectAtIndex:x]; 
		}
		
	}
#ifdef DEBUG
	NSLog(@"Entry not found in nuclearDataArray for neutron count %i and proton count %i",theNeutronCount,theProtonCount);
#endif

	return nil;
}



- (Nuclide *) entryForNucleonCount:(int) theNucleonCount
						NeutronCount:(int) theNeutronCount
{
#ifdef DEBUG
	NSLog(@"entering method entryForNucleonCount:NeutronCount:");
#endif

	if (theNucleonCount == nil || theNeutronCount == nil) {
#ifdef DEBUG
		NSLog(@"EPIC FAIL! theNucleonCount or theNeutronCount IS NIL!");
#endif

		return nil;
	}
	int theCount = [nuclearDataArray count];
#ifdef DEBUG
	NSLog(@"Number of items in array: %i",theCount);
#endif
	
	for (int x =0;x<theCount;x++)
	{
		if ([[nuclearDataArray objectAtIndex:x] nucleonCount] == theNucleonCount &&
			[[nuclearDataArray objectAtIndex:x] neutronCount] == theNeutronCount) {
#ifdef DEBUG
			NSLog(@"\t\t\tFOUND IT!!!!");
			NSLog(@"%i",x);
#endif
			return [nuclearDataArray objectAtIndex:x]; 
		}
		
	}
#ifdef DEBUG
	NSLog(@"Entry not found in nuclearDataArray for nucleon count %i and neutron count %i",theNucleonCount,theNeutronCount);
#endif
	return nil;
}



- (Nuclide *) entryForNeutronCount:(int) theNeutronCount
						  ElementSymbol:(NSString *)theElementSymbol
{
#ifdef DEBUG
	NSLog(@"entering method entryForNeutronCount:ElementSymbol:");
#endif
	if (theNeutronCount == nil || theElementSymbol == nil) {
#ifdef DEBUG
		NSLog(@"EPIC FAIL! theNeutronCount or theElementSymbol IS NIL!");
#endif
		return nil;
	}
	int theCount = [nuclearDataArray count];
#ifdef DEBUG
	NSLog(@"Number of items in array: %i",theCount);
#endif
	
	for (int x =0;x<theCount;x++)
	{
		if ([[nuclearDataArray objectAtIndex:x] neutronCount] == theNeutronCount &&
			[theElementSymbol caseInsensitiveCompare:[[nuclearDataArray objectAtIndex:x] elementSymbol]] == 0) {
#ifdef DEBUG
			NSLog(@"\t\t\tFOUND IT!!!!");
			NSLog(@"%i",x);
#endif
			return [nuclearDataArray objectAtIndex:x]; 
		}
		
	}
#ifdef DEBUG
	NSLog(@"Entry not found in nuclearDataArray for neutron count %i and element symbol %@",theNeutronCount,theElementSymbol);
#endif
	return nil;
}



- (Nuclide *) entryForNucleonCount:(int) theNucleonCount
						  ElementSymbol:(NSString *)theElementSymbol
{
#ifdef DEBUG
	NSLog(@"entering method entryForNucleonCount:ElementSymbol:");
#endif
	if (theNucleonCount == nil || theElementSymbol == nil) {
#ifdef DEBUG
		NSLog(@"EPIC FAIL! theNucleonCount or theElementSymbol IS NIL!");
#endif
		return nil;
	}
	int theCount = [nuclearDataArray count];
#ifdef DEBUG
	NSLog(@"Number of items in array: %i",theCount);
#endif
	
	for (int x =0;x<theCount;x++)
	{
		if ([[nuclearDataArray objectAtIndex:x] nucleonCount] == theNucleonCount &&
			[theElementSymbol caseInsensitiveCompare:[[nuclearDataArray objectAtIndex:x] elementSymbol]] == 0) {
#ifdef DEBUG
			NSLog(@"\t\t\tFOUND IT!!!!");
			NSLog(@"%i",x);
#endif
			return [nuclearDataArray objectAtIndex:x]; 
		}
		
	}
#ifdef DEBUG
	NSLog(@"Entry not found in nuclearDataArray for nucleon count %i and element symbol %@",theNucleonCount,theElementSymbol);
#endif
	return nil;
}


//	validate is intended to search the NuclideTable for conflicting Nuclide entries
//	(same z, different element names, for example.)
//	Not implimented at this time.
- (BOOL) validate
{
	return NO;
}





@synthesize scanner;
@synthesize nuclearDataArray;

@end
